﻿
var validate = new Validate();

$(document).ready(function () {
    $("#btnSendCaptcha").click(function () {
        if (!validate.CheckPhone($(".txtPhone").val())) {
            $(".txtPhone").focus();
            alert("手机号码不正确！");
            return false;
        }
        if ($("#userCode").val() == null || $("#userCode").val() == "") {
            alert("验证码不能为空!");
            return false;
        }

        //var i = validate.CheckIdCard($(".txtIDCard").val());
        //if (i < 0) {
        //    $(".txtIDCard").focus();
        //    alert("无效的身份证号码！");
        //}       
        $.ajax({
            url: "../Handler/InvestLoginHandler.ashx",
            type: "post",
            dataType: "json",
            data: { type: 0, Phone: $(".txtPhone").val(), IdCard: $(".txtIDCard").val() },
            async: true,
            complete: function () { },
            success: function (result) {
                if (result.Status) {
                    if (getVerifyCode()) {
                        startTimer();
                    }
                }
                else {
                    $(".btnSendCaptcha").removeAttr("disabled");
                }
                alert(result.Message);
            }
        });
    });

    $(".login").click(function () {
        if (!validate.CheckPhone($(".txtPhone").val())) {
            $(".txtPhone").focus();
            alert("手机号码不正确！");
            return false;
        }
        //var i = validate.CheckIdCard($(".txtIDCard").val());
        //if (i < 0) {
        //    $(".txtIDCard").focus();
        //    alert("无效的身份证号码！");
        //    return false;
        //}
        if ($(".txtCaptcha").val() == "") {
            $(".txtCaptcha").focus();
            alert("验证码不能为空！");
            return false;
        }
        $.ajax({
            url: "../Handler/InvestLoginHandler.ashx",
            type: "post",
            dataType: "json",
            data: { type: 1, Phone: $(".txtPhone").val(), IdCard: $(".txtIDCard").val(), Captcha: $(".txtCaptcha").val() },
            async: true,
            complete: function () { },
            success: function (result) {
                if (!result.Status) {
                    alert(result.Message);
                }
                else {
                    //alert("Hello World!");
                    //判断浏览器是否支持localStorage缓存
                    if (window.localStorage) {
                        localStorage.setItem("Phone", result.Data.Phone);
                        localStorage.setItem("IdCard", result.Data.IdCard);
                        localStorage.setItem("Cryptographic", result.Data.Cryptographic);
                        location.href = "FinaInvestList.aspx?css=3053";
                    }
                    else {
                        location.href = "FinaInvestList.aspx?css=3053&Phone=" + result.Data.Phone + "&IdCard=" + result.Data.IdCard + "&Cryptographic=" + result.Data.Cryptographic;
                    }
                }

            }
        });
    });
});


//计时器
function startTimer() {
    var text = $(".btnSendCaptcha").val();
    var reg = new RegExp(/\d+$/);
    $(".btnSendCaptcha").attr("disabled", "disabled").val(text + 120);
    $(".btnSendCaptcha").addClass("verification_ip_no");
    var timer = setInterval(function () {
        var time = parseInt(reg.exec($(".btnSendCaptcha").val()));
        time--;
        if (time <= 0) {
            $(".btnSendCaptcha").val(text).removeAttr("disabled");
            window.clearInterval(timer);
            $(".btnSendCaptcha").removeClass("verification_ip_no");
        }
        else {
            $(".btnSendCaptcha").val(text + time);
        }
    }, 1000);
}

//获取验证码图片
function getImgCode() {
    var randCodeImg = document.getElementById("randCodeId");
    randCodeImg.src = "/Handler/CodeHandler.ashx?rand=" + Math.random();
}


//获取验证码
function getVerifyCode() {
    var userCode = $("#userCode").val().toUpperCase();
    $.ajax({
        url: "/Handler/CodeHandler.ashx",
        type: "post",
        dataType: "json",
        data: { type: "GetCode" },
        async: true,
        complete: function () { },
        success: function (data) {
            if (data.Message == userCode.toString()) {
                return true;
            } else {
                alert("验证码不正确，请从新检查。");
                return false;
            }
        }
    });
}